package com.slw.caiji.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slw.caiji.Vo.params.PageParams;
import com.slw.caiji.dto.SysMenu;
import com.slw.caiji.dto.SysMenu;
import com.slw.caiji.mapper.SysMenuMapper;
import com.slw.caiji.service.SysMenuService;
import com.slw.caiji.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysMenuServiceImpl implements SysMenuService {


    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public Result getSysMenuList(PageParams pageParams) {
        final Page<SysMenu> objectPage = new Page<>(pageParams.getCurrent(), pageParams.getPageSize());
        final IPage<SysMenu> sysUserIPage = sysMenuMapper.listSysMenu(objectPage);
        return Result.success(sysUserIPage);
    }


    @Override
    public Result getSysMenuParentList(String name) {
        return Result.success(sysMenuMapper.getParentList(name));
    }

    @Override
    public Result addMenu(SysMenu sysMenu) {
        final SysMenu sysMenu1 = new SysMenu();
        sysMenu1.setName(sysMenu.getName());
        sysMenu1.setParentId(sysMenu.getParentId());
        sysMenu1.setCreatetime(new Date());
        sysMenu1.setUrl(sysMenu.getUrl());
        sysMenu1.setPerms(sysMenu.getPerms());
        sysMenu1.setIcon(sysMenu.getIcon());
        sysMenu1.setOrderNum(sysMenu.getOrderNum());
        return Result.success(sysMenuMapper.insert(sysMenu1));
    }

    @Override
    public Result delMenu(String ids) {
        final String[] split = ids.split(",");
        for (String s : split) {
            sysMenuMapper.deleteById(s);
        }
        return Result.success(1);
    }

    @Override
    public Result updateMenu(SysMenu sysMenu) {
        SysMenu sysMenu2 = new SysMenu();
        sysMenu2.setName(sysMenu.getName());
        sysMenu2.setCreatetime(new Date());
        sysMenu2.setParentId(sysMenu.getParentId());
        sysMenu2.setUrl(sysMenu.getUrl());
        sysMenu2.setPerms(sysMenu.getPerms());
        sysMenu2.setIcon(sysMenu.getIcon());
        sysMenu2.setOrderNum(sysMenu.getOrderNum());
        sysMenu2.setId(sysMenu.getId());
        return Result.success(sysMenuMapper.updateById(sysMenu2));
    }

    @Override
    public Result detailMenu(String id) {
        SysMenu sysMenu = sysMenuMapper.selectById(id);
        return Result.success(sysMenu);
    }

}
